package siafi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import siafi.bean.Concepto;
import siafi.bean.PersonaCentro;
import siafi.bean.Servicio;
import siafi.bean.Vinculacion;
import siafi.db.TerceroDAO;
import siafi.util.Administracion;

/**
 * Servlet implementation class CantidadServicio
 */
public class CantidadServicio extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CantidadServicio() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		@SuppressWarnings("unchecked")
		
		String concepto=null; 
		String vinculacion=null; 
		String row = request.getParameter("row");
		Boolean aut = false;
		if ((Boolean) request.getSession().getAttribute("aut") != null && (Boolean) request.getSession().getAttribute("aut")){
			aut = true;			
		}
		Boolean autEst = false;
		if ((Boolean) request.getSession().getAttribute("autEst") != null && (Boolean) request.getSession().getAttribute("autEst")){
			autEst = true;			
		}
		if(row==null){
		 concepto = request.getParameter("centroCosto");
		request.getSession().setAttribute("Concepto", TerceroDAO.findConcepto(concepto));
		vinculacion = request.getParameter("vinculacion");
			request.getSession().setAttribute("vinculacion", vinculacion);
		}
		else{
			 concepto =((Concepto)request.getSession().getAttribute("Concepto")).getCod_concepto();
		}
		
		String idu_documento = request.getParameter("vigentes");
		/*while(nombres.hasMoreElements()){
			ntemp = nombres.nextElement();
			if(ntemp.indexOf("servicio-") != -1)
				servicios.add(ntemp.substring(ntemp.indexOf("-")+1));
		}
		String servicio = request.getParameter("servicio");*/
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
		out.println("<html>");
		out.println(" <head>");
		out.println(" <link href=\"./css/coolblue.css\" rel=\"stylesheet\" type=\"text/css\"/>");
		out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
		out.println("  <title>Recibos</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("   <div align=\"center\" id=\"header\">");
		out.println(" <img src=\"./images/upn.jpg\"/>");
		out.println(" </div>");
		long total = 0;
		if( request.getSession().getAttribute("tercero")!=null){
		    if(idu_documento != null && !"0".equals(idu_documento)){
		    	request.getSession().setAttribute("idu_documento", idu_documento);
		    	response.sendRedirect("/recibosSIAFI/GenerarReciboSNoVencido");
		    }else if (!aut && !autEst){
				response.sendRedirect("index.xhtml");				
		    }else if(concepto == null || "".equals(concepto)){
		    	request.getSession().setAttribute("conceptosM", "siMc");
		    	response.sendRedirect("/recibosSIAFI/ReciboServicio");
		    }else{
		    	
			out.println(" <div align=\"center\" id=\"content\">");
			out.println(" <form action=\"/recibosSIAFI/ResumenRecibo\" method=\"post\">");
			out.println("     <table>");
			out.println(" <tr>");
			out.println(" <td colspan=\"4\"><h3>Recibo de Servicio</h3></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"4\" align=\"left\"><h4 align=\"left\">"+((PersonaCentro)request.getSession().getAttribute("tercero")).getNombre()+"</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"4\" align=\"left\"><h4>POR FAVOR SELECCIONE LOS SERVICIOS QUE DESEA Y A CONTINUACIÓN LLENE LOS DATOS NECESARIOS. MAXIMO 7	TEMS</h4></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"4\" align=\"left\"><h3>Seleccione la cantidad de cada servicio a pagar</h3></td>");
			out.println("</tr>");
			List<Servicio> serviciol = null;
			if(request.getSession().getAttribute("servicios")==null || row == null){
				serviciol = TerceroDAO.findServicioGrilla( concepto, vinculacion);
				request.getSession().setAttribute("servicios", serviciol);
			} else{
				serviciol = (List<Servicio>)request.getSession().getAttribute("servicios");
				if(row != null){
					serviciol.remove(Integer.parseInt(row));
				}
			}
			if (autEst){
				List<String> servGuardados = new ArrayList<String>();
				if (request.getSession().getAttribute("servGuar") != null){
					servGuardados =(List<String>) request.getSession().getAttribute("servGuar");
				}
				List<Servicio> servEst = new ArrayList<Servicio>();
				for (String s : servGuardados){
					for(Servicio serv :serviciol){
						if(serv.getConcepto().equals(s)){
							servEst.add(serv);
						}
					}		
				}
				serviciol = servEst;
			}
			out.println(" <tr>");
			out.println(" <th>Servicio");
			out.println(" </th>");

			out.println(" <th>Valor Unitario");
			out.println(" </th>");

			out.println(" <th>Cantidad");
			out.println(" </th>");

			out.println(" <th> ");
			out.println(" </th>");
			out.println(" </tr>");
			int i = 0;
			for(Servicio s :serviciol){
				out.println(" <tr>");
				out.println(" <td><label>"+s.getConcepto()+"</label></td>");
				if(s.getValor_unitario().intValue()>0)
					out.println(" <td> <input type=\"text\" size=\"10\" name=\"valor-"+s.getCod_concepto()+"\" readonly=\"readonly\" value=\""+s.getValor_unitario()+"\"> </td>");
				else{
					out.println(" <td> ");
					out.println("  <input type=\"text\" size=\"3\" name=\"valor-"+s.getCod_concepto()+"\" value=\""+s.getValor_unitario()+"\"> ");
					out.println("</td>");	
				}
				
				out.println(" <td> ");
				if (s.getConcepto().equals("almuerzos estudiantes subsidiados")){
					out.println(" <td> <select name=\"servicio-"+s.getCod_concepto()+"\" value=\""+s.getCan_liquidacion()+">");
					for (int al = 0; al<66; al++) {
						out.println("  <option value=\"" + al +"\">" + al + "</option> ");
					}

					out.println("</select></td>");
					
				} else
				out.println("  <input type=\"text\" size=\"3\" name=\"servicio-"+s.getCod_concepto()+"\" value=\""+s.getCan_liquidacion()+"\"> ");
				out.println("</td>");
				out.println(" <td> ");				
				out.println(" <input type=\"checkbox\" name=\"conceptos\" value=\""+s.getConcepto()+"\"><br>");			
				out.println("</td>");
				out.println("</tr>");
				total += s.getValor_unitario();
				i++;
			}			
			out.println(" <tr>");
			out.println(" <td><button style=\"border-color: transparent\" type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\"><img src=\"images/btn_regresar.gif\"/></button></td>");
			out.println("  <td><button style=\"border-color: transparent\" type=\"submit\" class=\"button\"><img src=\"images/btn_aceptar.gif\"/></button></td>");
			out.println(" </tr>");
			out.println(" <tr>");			
			out.println(" <td colspan=\"1\"><a href=\"indexServicio.jsp\"><img src=\"images/btn_vINICIO.gif\"/></a></td>");			
			out.println("  <td><a href=\"Logout\"><img alt=\"salir\" src=\"images/btn_terminar.gif\"/> </a></td>");
			
			out.println(" </tr>");
			out.println("</table>");
			out.println("</form>");
			out.println("</div>");
		    }
		}
		out.println(" <div align=\"right\" id=\"footer\" style=\"width: 80%\">");
		out.println("<img src=\"./images/SiafiReportes.bmp\" width=\"100px\" height=\"30px\"/>");
		out.println("</div>");

		out.println("</body>");
		out.println("</html>");

		out.close();
	}
	
}
